#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

//CMAX means max cylinder
#define CMAX 199

void printArray(int* arr, int length) {
	for (int i = 0; i < length; ++i) {
		printf("%d ", arr[i]);
	}
	printf("\n");
}

void FCFS(int* input, int inputLength, int* output, int* outputLength) {
	
	for (int i = 0; i < inputLength; ++i) {
		output[i] = input[i];
	}
	*outputLength = inputLength;
	printf("FCFS: ");
	printArray(output, *outputLength);
}

void SSTF(int* input, int inputLength, int* output, int* outputLength) {
	
	int indexArray[20] = { 0 };

	int headNumber = input[0];
	int headIndex = 0;
	output[0] = headNumber;
	indexArray[0] = 1;
	for (int i = 1; i < inputLength; ++i) {
		int min_index = 0;
		int minNum = RAND_MAX;
		
		for (int j = 0; j < inputLength; ++j) {
			if (headIndex== j) continue;
			if (indexArray[j] == 1) continue;
			if (abs(input[j] - headNumber) < minNum) {
				min_index = j;
				minNum = abs(input[j] - headNumber);
			}
			
		}
		indexArray[min_index] = 1;
		headNumber = input[min_index];
		headIndex = min_index;
		output[i] = input[min_index];
	
	}
	*outputLength = inputLength;
	printf("SSTF: ");
	printArray(output, *outputLength);
}

void SCAN(int* input, int inputLength, int* output, int* outputLength) {

	int headNumber = input[0];
	int headIndex = 0;
	*outputLength = 0;
	for (int i = 0; i < inputLength; i++)
	{
		for (int j = i + 1; j < inputLength; ++j) {
			if (input[i] > input[j]) {
				int temp = input[i];
				input[i] = input[j];
				input[j] = temp;
				if (i == headIndex) headIndex = j;
				else if (j == headIndex) headIndex = i;
			}
		}
	}

	// output[(*outputLength)++] = 0;


	for (int j = headIndex + 1; j < inputLength; ++j) {
		output[(*outputLength)++] = input[j];
	}
	
	output[(*outputLength)++] = CMAX;

    for (int j = headIndex; j >= 0; --j) {
		output[(*outputLength)++] = input[j];		
	}
	
	
	output[(*outputLength)++] = 0;

    	printf("Sort Array: ");
    	printArray(input, inputLength);
	printf("SCAN: ");
	printArray(output, *outputLength);

}

void CSCAN(int* input, int inputLength, int* output, int* outputLength) {
	int headNumber = input[0];
	int headIndex = 0;
	*outputLength = 0;
	for (int i = 0; i < inputLength; i++)
	{
		for (int j = i + 1; j < inputLength; ++j) {
			if (input[i] > input[j]) {
				int temp = input[i];
				input[i] = input[j];
				input[j] = temp;
				if (i == headIndex) headIndex = j;
				else if (j == headIndex) headIndex = i;
			}
		}
	}

	for (int j = headIndex; j < inputLength; ++j) {
		output[(*outputLength)++] = input[j];
	}

	output[(*outputLength)++] = CMAX;
	output[(*outputLength)++] = 0;

	for (int j = 0; j < headIndex; ++j) {
		output[(*outputLength)++] = input[j];
	}

	printf("Sort Array: ");
    	printArray(input, inputLength);
	printf("CSCAN: ");
	printArray(output, *outputLength);
}

void LOOK(int* input, int inputLength, int* output, int* outputLength) {

	int headNumber = input[0];
	int headIndex = 0;
	*outputLength = 0;
	for (int i = 0; i < inputLength; i++)
	{
		for (int j = i + 1; j < inputLength; ++j) {
			if (input[i] > input[j]) {
				int temp = input[i];
				input[i] = input[j];
				input[j] = temp;
				if (i == headIndex) headIndex = j;
				else if (j == headIndex) headIndex = i;
			}
		}
	}



	for (int j = headIndex + 1; j < inputLength; ++j) {
		output[(*outputLength)++] = input[j];
	}
	
	for (int j = headIndex; j >= 0; --j) {
		output[(*outputLength)++] = input[j];

	}
	
	printf("Sort Array: ");
    	printArray(input, inputLength);

	printf("LOOK: ");
	printArray(output, *outputLength);

}


void CLOOK(int* input, int inputLength, int* output, int* outputLength) {
	int headNumber = input[0];
	int headIndex = 0;
	*outputLength = 0;
	for (int i = 0; i < inputLength; i++) {
		for (int j = i + 1; j < inputLength; ++j) {
			if (input[i] > input[j]) {
				int temp = input[i];
				input[i] = input[j];
				input[j] = temp;
				if (i == headIndex) headIndex = j;
				else if (j == headIndex) headIndex = i;
			}
		}
	}

	for (int j = headIndex; j < inputLength; ++j) {
		output[(*outputLength)++] = input[j];

	}

	for (int j = 0; j < headIndex; ++j) {
		output[(*outputLength)++] = input[j];
	}
	
	printf("Sort Array: ");
    	printArray(input, inputLength);

	printf("CLOOK: ");
	printArray(output, *outputLength);
}


int main(int argc, char const* argv[]) {

	//initial head is the first item
	int input[] = { 53, 98, 183, 37, 122, 14, 124, 65, 67 };

	int output[20];
	int output_length = 0;


	printf("input: ");
	printArray(input, 9);
	
	// FCFS(input, 9, output, &output_length);
	// SSTF(input, 9, output, &output_length);
	// SCAN(input, 9, output, &output_length);
	LOOK(input, 9, output, &output_length);
	//CSCAN(input, 9, output, &output_length);
	//CLOOK(input, 9, output, &output_length);
	

	return 0;
}
